package com.example.newdemo.activity.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.newdemo.R;

public class HomeActivity extends AppCompatActivity {
    Button openHomeActivityButton;
    Button openSecondActivityButton;
    Button openThirdActivityButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openHomeActivityButton = findViewById(R.id.openHomeActivity);
        openSecondActivityButton = findViewById(R.id.openSecondActivity);
        openThirdActivityButton = findViewById(R.id.openThirdActivity);

        openHomeActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("wwwwwww");
                intent.addCategory("android.intent.category.LAUNCHER");

                if (getAppInfo(intent)!=null){
                    startActivity(intent);
                    Log.v("error","启动");
                }else {
                    Log.v("error","没有知道指定活动");
                }

            }
        });
        openSecondActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
        openThirdActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this, ThirdActivity.class);
                startActivity(intent);
            }
        });
        Log.e("test", "HomeActivity活动被建立" + Thread.currentThread()+ "，在栈：" + getTaskId());
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        Log.e("test", "HomeActivity活动被销毁" + "，在栈：" + getTaskId());
    }

    private ResolveInfo getAppInfo(Intent intent) {
        // 获取PackageManager对象
        PackageManager pm = this.getPackageManager();

        // 通过queryIntentActivities获取ResolveInfo对象

        return pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
    }

}